package com.gin.custom.cust1;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * Created by db.boy on 2018/6/8.
 */
public class CustomPopupWindow extends PopupWindow {

    public CustomPopupWindow(final Activity context, View contentView, int width, int height) {
        super(contentView, width, height);
    }

    public CustomPopupWindow(final Activity context, View contentView) {
        super(context);

        // 弹出PopupWindow时让后面的界面变暗
        //        WindowManager.LayoutParams parms =context.getWindow().getAttributes();
        //        parms.alpha =0.5f;
        //        context.getWindow().setAttributes(parms);

        this.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // PopupWindow消失后让后面的界面变亮
                WindowManager.LayoutParams parms = context.getWindow().getAttributes();
                parms.alpha = 1.0f;
                context.getWindow().setAttributes(parms);

            }
        });

        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        this.setContentView(contentView);
        setBackgroundDrawable(new ColorDrawable(0x088600));
        contentView.measure(0, 0);
        this.setWidth(w);
        this.setHeight(contentView.getMeasuredHeight());
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.update();
    }


    public void show(View anchor) {
        int[] location = new int[2];
        //        anchor.getLocationInWindow(location); //获取在当前窗口内的绝对坐标
        anchor.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标
        Log.i("------", " an: " + anchor.getBottom());
        Log.i("------", " an: " + location[0] + "  y: " + location[1]);
        //        int newHeight = this.getHeight()-location[1]-anchor.getBottom();
        //        this.setHeight(newHeight);
        this.update();

        this.showAsDropDown(anchor, 0, 0);
    }
}
